﻿using OnlineCellphoneStore.Models;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace OnlineCellphoneStore.UserPages
{
    public partial class OrderSummary : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            UserContext();
            if (!Page.IsPostBack)
            {
                Order order = HandleOrder();
                int OrderId = WriteOrderToDatabse(order);
                WriteOrderDetails(OrderId, order);
            }
        }
        private Order HandleOrder()
        {
            Order order = new Order();
            order.ClientID = Int32.Parse(Session["ClientID"].ToString());
            order.Currency = Session["Currency"].ToString();
            order.TotalAmount = Double.Parse(Session["AmountToBuy"].ToString());
            order.OrderDate = DateTime.Now;
            order.ShipingDate = (DateTime)Session["Calender"];
            DataTable cart = (DataTable)Session["Cart"];
            foreach (DataRow row in cart.Rows)
            {
                int PhoneID = (int)row["ID"];
                int WantedUnits = (int)row["Units Selected"];
                int AvailableUnits = (int)row["Available Units"];
                order.CellphonesToUnitsPurcahse.Add(PhoneID, WantedUnits);
                order.CellphonesToUpdatedUnits.Add(PhoneID, AvailableUnits - WantedUnits);
            }
            return order;
        }
        private int WriteOrderToDatabse(Order order)
        {
            try
            {
                int id = SQLAdapter.InsertOrder(order);
                return id;
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
                return 0;
            }
        }
        private void WriteOrderDetails(int OrderId, Order order)
        {
            OrderNumberHeader.InnerHtml += OrderId.ToString();
            DataTable cart = (DataTable)Session["Cart"];
            DataTable details = new DataTable();
            details.Columns.Add("Order Date", typeof(DateTime));
            details.Columns.Add("Shipment Date", typeof(DateTime));
            details.Columns.Add("Currency", typeof(string));
            details.Columns.Add("Total Price", typeof(double));
            DataRow dr = details.NewRow();
            dr["Order Date"] = order.OrderDate;
            dr["Shipment Date"] = order.ShipingDate;
            dr["Currency"] = order.Currency;
            dr["Total Price"] = order.TotalAmount;
            details.Rows.Add(dr);

            DetailsGridView.DataSource = details;
            CellInOrderGridView.DataSource = cart;
            DetailsGridView.DataBind();
            CellInOrderGridView.DataBind();

            Session.Remove("Calender");
            Session.Remove("Currency");
            Session.Remove("AmountToBuy");
            Session.Remove("TotalAmount");
            Session.Remove("Cart");
        }
        protected void LogOut_Click(object sender, EventArgs e)
        {
            string Url = "../Main.aspx?LogOut=true";
            Response.Redirect(Url, false);
        }
        private void HandleUserLables()
        {
            // Update labels
            aHelloUser.InnerHtml += Session["UserName"].ToString();
            aLastEntered.InnerHtml += Session["LastVisit"].ToString();
        }
        private void UserContext()
        {
            HandleUserLables();
        }
        protected void BtnBackToMain_Click(object sender, EventArgs e)
        {
            string Url = "../Main.aspx";
            Response.Redirect(Url, false);
        }
    }
}